<template>
	<view class="maintenInfo">
		<u-navbar title="设备维修" back-icon-color="#fff"
			:background="{backgroundImage: 'linear-gradient(45deg, #68dae7, #21a4b3)'}" title-color="#FFFFFF">
		</u-navbar>
		<view class="maintenInfo-form">
			<u-form :model="maintenInfo" label-width="180rpx" >
				<u-form-item label="资产编号"><u-input v-model="serialNumber" disabled/></u-form-item>
				<u-form-item label="设备名称"><u-input v-model="deviceName" disabled/></u-form-item>
				<u-form-item label="故障原因">
					<u-input type="textarea" :height="240" v-model="applyReason" disabled/>
				</u-form-item>
				<u-form-item label="维修人"><u-input v-model="repairPeople" disabled/></u-form-item>
				<!-- <u-form-item label="维修时间"><view style="width: 100%;"  @tap="showTime = true">{{maintenInfo.time}}</view></u-form-item> -->
				<u-form-item label="维修过程">
					<u-input type="textarea" :height="240" v-model="maintenInfo.process" placeholder=" "/>
				</u-form-item>
				<u-form-item label="维修配件">
					<u-input type="textarea" :height="240" v-model="maintenInfo.spareParts" placeholder=" "/>
				</u-form-item>
				
			</u-form>
			<view class="" v-if="maintenInfo.status == 4">
				<view class="maintenInfo-form-upload" >查看报告</view>
				<image class="devicePng" mode="widthFix" @click="previewImage(maintenInfo.report)" :src="maintenInfo.report"></image>
			</view>
			<view class="" style="margin-bottom: 120rpx;" v-else>
				<view class="maintenInfo-form-upload" >上传报告</view>
				<u-upload ref="uUpload" :max-count="1" :action="action" @on-error="uploadError" @on-success="uploadSuccess" :header="uploadHeader" :auto-upload="false" ></u-upload>
			</view>
		</view>
		<view class="maintenInfo-btn" v-if="maintenInfo.status !=4">
			<view class="maintenInfo-btn-sumbit" @tap='startCheck'>
				维修开始
			</view>
			<view class="maintenInfo-btn-pause" @tap='puseCheck'>
				暂停
			</view>
			<view class="maintenInfo-btn-sumbit" @tap='successCheck'>
				完成
			</view>
		</view>
		<u-picker mode="time" v-model="showTime" @confirm = "chosseTime"></u-picker>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default{
		data(){
			return {
				repairPeople:uni.getStorageSync("userName"),
				maintenInfo:{
				},
				deviceSn:"",
				serialNumber:"",
				deviceName:"",
				applyReason:"",
				showTime:false,
				action:uni.getStorageSync("baseUrl")+"/wx/upload",
				uploadHeader:{
					"Content-Type": "multipart/form-data",
					"token":uni.getStorageSync("token"),
				}
			}
		},
		onLoad(options) {
			this.deviceSn = options.deviceSn
			this.serialNumber = options.serialNumber
			this.deviceName = options.deviceName
			this.applyReason = options.applyReason
			this.getData()
		},
		methods:{
			getData(){
				let params = {
					id:this.deviceSn
				}
				this.$xt.api.deviceCheckDetail(params).then(res=>{
					this.maintenInfo = res.data
				})
			},
			chosseTime(val){
				this.maintenInfo.time = val.year+'-'+val.month+'-'+val.day
			},
			startCheck(){
				let params = {
					id: this.deviceSn,
					status: 2,
					startTime: this.$xt.util.getNowTime()
				}
				this.$xt.api.deviceCheckEdit(params).then(res=>{
					let page = getCurrentPages(),
					currentPages = page[page.length - 2]
					currentPages.$vm.reloadList()
					this.$refs.uToast.show({
						title:res.msg,
						duration:2000,
						type:"success"
					})
				})
			},
			puseCheck(){
				let params = this.maintenInfo
				params.id= this.deviceSn
				params.status= 3
				params.finishTime= this.$xt.util.getNowTime()
				
				if(this.$refs.uUpload.lists.length>0){
					this.$refs.uUpload.upload()
				}else{
					this.saveInfo(params)
				}
			},
			successCheck(){
				let params = this.maintenInfo
				params.id= this.deviceSn
				params.status= 4
				params.finishTime= this.$xt.util.getNowTime()
				
				if(this.$refs.uUpload.lists.length>0){
					this.$refs.uUpload.upload()
				}else{
					this.saveInfo(params)
				}
			},
			uploadSuccess(data, index, lists, name){
				let params = this.maintenInfo
				params.id= this.deviceSn
				params.status= 4
				params.repairPeople = this.repairPeople
				params.finishTime= this.$xt.util.getNowTime()
				params.report= data.url
				this.saveInfo(params)
			},
			uploadError(data){
				this.$refs.uToast.show({
					title:data.msg,
					duration:2000,
					type:"error"
				})
			},
			saveInfo(params){
				this.$xt.api.deviceCheckEdit(params).then(res=>{
					if(params.status== 3 || params.status== 4) this.goBack()
					this.$refs.uToast.show({
						title:res.msg,
						duration:2000,
						type:"success"
					})
				})
			},
			previewImage(url){
				uni.previewImage({
					current: url,
					urls:[url]
				})
			},
			goBack(){
				let page = getCurrentPages(),
				currentPages = page[page.length - 2]
				setTimeout(()=>{
					currentPages.$vm.reloadList()
					this.$u.route({type: 'back'})
				},1000)
			}
		}
	}
</script>

<style scoped lang="scss">
	.maintenInfo{
		position: relative;
		&-form{
			background-color: #fff;
			padding: 10rpx 24rpx;
			margin-bottom: 50rpx;
			&-upload{
				padding: 20rpx 0;
			}
		}
		&-btn{
			position: fixed;
			bottom: 0;
			padding:20rpx 40rpx;
			width: 100%;
			background-color: #fff;
			display: flex;
			justify-content: space-around;
			&-pause{
				background-color: #00aaff;
				text-align: center;
				padding: 10rpx 24rpx;
				border-radius: 15rpx;
				color:#fff
			}
			&-sumbit{
				background-color: #00aa00;
				text-align: center;
				padding: 10rpx 24rpx;
				border-radius: 15rpx;
				color:#fff
			}
		}
	}
</style>